home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mint108s.zoo / dosmem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-05  |  34.4 KB  |  1,344 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith.
  3. Copyright 1992,1993 Atari Corporation.
  4. All rights reserved.
  5. */
  6.  
  7. /*
  8.  * GEMDOS emulation routines: these are for the GEMDOS system calls
  9.  * concerning allocating/freeing memory, including Pexec() (since
  10.  * this allocates memory) and Pterm() (since this, implicitly, frees
  11.  * it).
  12.  */
  13.  
  14. #include "mint.h"
  15.  
  16. #define DIRSEP(c) ((c) == '\\')
  17.  
  18. int procdate, proctime;    /* set when any processes are created/destroyed */
  19.  
  20. static long do_vfork P_((int));
  21.  
  22. /*
  23.  * new call for TT TOS, for the user to inform DOS of alternate memory
  24.  * FIXME: we really shouldn't trust the user so completely
  25.  * FIXME: doesn't work if memory protection is on
  26.  */
  27.  
  28. long ARGS_ON_STACK
  29. m_addalt(start, size)
  30.     long start, size;
  31. {
  32.     extern int no_mem_prot;        /* see main.c and memprot.c */
  33.  
  34.     if (!no_mem_prot) return 0;    /* pretend to succeed */
  35.     if (!add_region(alt, start, size, M_ALT))
  36.         return EINTRN;
  37.     else
  38.         return 0;
  39. }
  40.  
  41. /*
  42.  * internal routine for doing Malloc on a particular memory map
  43.  */
  44.  
  45. long
  46. _do_malloc(map, size, mode)
  47.     MMAP map;
  48.     long size;
  49.     int mode;
  50. {
  51.     virtaddr v;
  52.     MEMREGION *m;
  53.     long maxsize, mleft;
  54.  
  55.     if (size == -1L) {
  56.         maxsize = max_rsize(map);
  57.         if (curproc->maxmem) {
  58.             mleft = curproc->maxmem - memused(curproc);
  59.             if (maxsize > mleft)
  60.                 maxsize = mleft;
  61.             if (maxsize < 0)
  62.                 maxsize = 0;
  63.         }
  64.     /* make sure to round down */
  65.         return maxsize & ~MASKBITS;
  66.     }
  67.  
  68. /* special case: Malloc(0) should always return 0 */
  69.     if (size == 0)
  70.         return 0;
  71.  
  72.     if (curproc->maxmem) {        /* memory limit? */
  73.         if (size > curproc->maxmem - memused(curproc)) {
  74.             DEBUG(("malloc: memory request would exceed limit"));
  75.             return 0;
  76.         }
  77.     }
  78.  
  79.     m = get_region(map, size, mode);
  80.     if (!m) {
  81.         return 0;
  82.     }
  83.     v = attach_region(curproc, m);
  84.     if (!v) {
  85.         m->links = 0;
  86.         free_region(m);
  87.         return 0;
  88.     }
  89. /* NOTE: get_region returns a region with link count 1; since attach_region
  90.  * increments the link count, we have to remember to decrement the count
  91.  * to correct for this.
  92.  */
  93.     m->links--;
  94.     if ((mode & F_KEEP)) {    /* request for permanent memory */
  95.         m->mflags |= M_KEEP;
  96.     }
  97.     return (long)v;
  98. }
  99.  
  100. long ARGS_ON_STACK
  101. m_xalloc(size, mode)
  102.     long size;
  103.     int mode;
  104. {
  105.     long r, r1;
  106.     int protmode;
  107. #ifdef DEBUG_INFO
  108.     int origmode = mode;
  109. #endif
  110.  
  111.     TRACE(("Mxalloc(%ld,%x)",size,mode));
  112.  
  113. /*
  114.  * AKP: Hack here: if the calling process' PC is in ROM, then this is a
  115.  * Malloc call made by VDI's v_opnvwk routine.  So we change mode to
  116.  * include "super accessible."  This is temporary, until VDI catches up
  117.  * with multitasking TOS.
  118.  */
  119.  
  120.     if (((mode & F_PROTMODE) == 0) &&
  121.         (curproc->ctxt[SYSCALL].pc > 0x00e00000L) &&
  122.         (curproc->ctxt[SYSCALL].pc < 0x00efffffL)) {
  123.         mode |= (F_PROT_S + 0x10) | F_KEEP;
  124.         TRACE(("m_xalloc: VDI special (call from ROM)"));
  125.     }
  126. /*
  127.  * If the mode argument comes in a zero, then set it to the default
  128.  * value from prgflags.  Otherwise subtract one from it to bring it
  129.  * into line with the actual argument to alloc_region.
  130.  */
  131.     protmode = (mode & F_PROTMODE) >> F_PROTSHIFT;
  132.  
  133.     if (protmode == 0) {
  134.         protmode = (curproc->memflags & F_PROTMODE) >> F_PROTSHIFT;
  135.     }
  136.     else --protmode;
  137.  
  138. #if 0
  139. /* I'm very suspicious of the 0x08 flag; I can't see how it could
  140.  * work as the comment below seems to indicate -- ERS
  141.  */
  142.  
  143. /*
  144.  * if the mode argument has the 0x08 bit set then you're trying to change
  145.  * the protection mode of a block you already own. "size" is really its
  146.  * base address. (new as of 2/6/92)
  147.  */
  148.     if (mode & 0x08) change_prot_status(curproc,size,protmode);
  149. #endif
  150.  
  151.     /*
  152.      * Copy the F_KEEP attribute into protmode.  We didn't do that
  153.      * before now because change_prot_status don't want to see no
  154.      * steenking nofree attributes.
  155.      */
  156.  
  157.     protmode |= (mode & F_KEEP);
  158.  
  159.     /* mask off all but the ST/alternative RAM bits before further use */
  160.     mode &= 3;
  161.  
  162.     if (mode == 0) {
  163.         r = _do_malloc(core, size, protmode);
  164.         goto ret;
  165.     }
  166.     else if (mode == 1) {
  167.         r = _do_malloc(alt, size, protmode);
  168.         goto ret;
  169.     }
  170.     else if (size == -1) {
  171.         /* modes 2 and 3 are the same for for size -1 */
  172.         r = _do_malloc(core, -1L, PROT_P);
  173.         r1 = _do_malloc(alt, -1L, PROT_P);
  174.         if (r1 > r) r = r1;
  175.         goto ret;
  176.     }
  177.     else if (mode == 2) {
  178.         r = _do_malloc(core, size, protmode);
  179.         if (r == 0) r = _do_malloc(alt, size, protmode);
  180.         goto ret;
  181.     }
  182.     else /* if (mode == 3) */ {
  183.         r = _do_malloc(alt, size, protmode);
  184.         if (r == 0) r = _do_malloc(core, size, protmode);
  185.         goto ret;
  186.     }
  187. ret:
  188.     if (r == 0) {
  189.         DEBUG(("m_xalloc(%lx,%x) returns 0",size,origmode));
  190.     } else {
  191.         TRACE(("m_xalloc(%lx,%x) returns %lx",size,origmode,r));
  192.     }
  193.     return r;
  194. }
  195.  
  196. long ARGS_ON_STACK
  197. m_alloc(size)
  198.     long size;
  199. {
  200.     long r;
  201.  
  202.     TRACE(("Malloc(%lx)", size));
  203.     if (curproc->memflags & F_ALTALLOC)
  204.         r = m_xalloc(size, 3);
  205.     else
  206.         r = m_xalloc(size, 0);
  207.     TRACE(("Malloc: returning %lx", r));
  208.     return r;
  209. }
  210.  
  211. long ARGS_ON_STACK
  212. m_free(block)
  213.     virtaddr block;
  214. {
  215.     MEMREGION *m;
  216.     int i;
  217.  
  218.     TRACE(("Mfree(%lx)", block));
  219.     if (!block) {
  220.         DEBUG(("Mfree: null pointer"));
  221.         return EIMBA;
  222.     }
  223.  
  224. /* search backwards so that most recently allocated incarnations of
  225.    shared memory blocks are freed first (this doesn't matter very often)
  226.  */
  227.  
  228.     for (i = curproc->num_reg - 1; i >= 0; i--) {
  229.         if (curproc->addr[i] == block) {
  230.             m = curproc->mem[i];
  231.             assert(m != NULL);
  232.             assert(m->loc == (long)block);
  233.             curproc->mem[i] = 0;
  234.             curproc->addr[i] = 0;
  235.             m->links--;
  236.             if (m->links == 0) {
  237.                 free_region(m);
  238.             }
  239.             return 0;
  240.         }
  241.     }
  242.  
  243. /* hmmm... if we didn't find the region, perhaps it's a global
  244.  * one (with the M_KEEP flag set) belonging to a process that
  245.  * terminated
  246.  */
  247.     for (i = rootproc->num_reg - 1; i >= 0; i--) {
  248.         if (rootproc->addr[i] == block) {
  249.             m = rootproc->mem[i];
  250.             assert(m != NULL);
  251.             assert(m->loc == (long)block);
  252.             if (!(m->mflags & M_KEEP))
  253.                 continue;
  254.             TRACE(("Freeing M_KEPT memory"));
  255.             rootproc->mem[i] = 0;
  256.             rootproc->addr[i] = 0;
  257.             m->links--;
  258.             if (m->links == 0) {
  259.                 free_region(m);
  260.             }
  261.             return 0;
  262.         }
  263.     }
  264.  
  265.  
  266.     DEBUG(("Mfree: bad address %lx", block));
  267.     return EIMBA;
  268. }
  269.  
  270. long ARGS_ON_STACK
  271. m_shrink(dummy, block, size)
  272.     int dummy;
  273.     virtaddr block;
  274.     long size;
  275. {
  276.     MEMREGION *m;
  277.     int i;
  278.  
  279.     UNUSED(dummy);
  280.     TRACE(("Mshrink: %lx to %ld", block, size));
  281.     if (!block) {
  282.         DEBUG(("Mshrink: null pointer"));
  283.         return EIMBA;
  284.     }
  285.  
  286.     for (i = 0; i < curproc->num_reg; i++) {
  287.         if (curproc->addr[i] == block) {
  288.             m = curproc->mem[i];
  289.             assert(m != NULL);
  290.             assert(m->loc == (long)block);
  291.             return shrink_region(m, size);
  292.         }
  293.     }
  294.     DEBUG(("Mshrink: bad address (%lx)", block));
  295.     return EIMBA;
  296. }
  297.  
  298. long ARGS_ON_STACK
  299. p_exec(mode, ptr1, ptr2, ptr3)
  300.     int mode;
  301.     void *ptr1, *ptr2, *ptr3;
  302. {
  303.     MEMREGION *base,
  304.         *env = 0;    /* assignment suppresses spurious warning */
  305.     MEMREGION *text = 0;    /* for shared text regions */
  306.     PROC *p;
  307.     long r, flags = 0;
  308.     int i;
  309.     char mkbase = 0, mkload = 0, mkgo = 0, mkwait = 0, mkfree = 0;
  310.     char overlay = 0;
  311.     char thread = 0;
  312.     char ptrace;
  313.     char mkname = 0, *newname, *lastslash;
  314.     char localname[PNAMSIZ+1];
  315.     XATTR xattr;
  316.     int newpid;
  317.  
  318. #ifdef DEBUG_INFO
  319. /* tfmt and tail_offs are used for debugging only */
  320.     const char *tfmt = "Pexec(%d,%s,\"%s\",%lx)";
  321.     int tail_offs = 1;
  322. #endif
  323.  
  324. /* the high bit of mode controls process tracing */
  325.     switch(mode & 0x7fff) {
  326.     case 0:
  327.         mkwait = 1;        /* fall through */
  328.     case 100:
  329.         mkload = mkgo = mkfree = 1;
  330.         mkname = 1;
  331.         break;
  332.     case 200:            /* overlay current process */
  333.         mkload = mkgo = 1;
  334.         overlay = mkname = 1;
  335.         break;
  336.     case 3:
  337.         mkload = 1;
  338.         break;
  339.     case 6:
  340.         mkfree = 1;
  341.         /* fall through */
  342.     case 4:
  343.         mkwait = mkgo = 1;
  344.         thread = (mode == 4);
  345. #ifdef DEBUG_INFO
  346.         tfmt = "Pexec(%d,%lx,BP:%lx,%lx)";
  347.         tail_offs = 0;
  348. #endif
  349.         break;
  350.     case 106:
  351.         mkfree = 1;        /* fall through */
  352.     case 104:
  353.         thread = (mode == 104);
  354.         mkgo = 1;
  355.         mkname = (ptr1 != 0);
  356. #ifdef DEBUG_INFO
  357.         tfmt = "Pexec(%d,%s,BP:%lx,%lx)";
  358.         tail_offs = 0;
  359. #endif
  360.         break;
  361.     case 206:
  362. #if 0
  363.     /* mkfree has no effect when overlay is set, since
  364.      * in this case the "parent" and "child" are the same
  365.      * process; since the "child" will run in memory that the
  366.      * "parent" allocated, we don't want that memory freed!
  367.      */
  368.         mkfree = 1;
  369. #endif
  370.         /* fall through */
  371.     case 204:
  372.         mkgo = overlay = 1;
  373.         mkname = (ptr1 != 0);
  374. #ifdef DEBUG_INFO
  375.         tfmt = "Pexec(%d,%s,BP:%lx,%lx)";
  376.         tail_offs = 0;
  377. #endif
  378.         break;
  379.     case 7:
  380.         flags = (long)ptr1;    /* set program flags */
  381.                     /* and fall through */
  382.     case 5:
  383.         mkbase = 1;
  384. #ifdef DEBUG_INFO
  385.         tfmt = "Pexec(%d,%lx,%s,%lx)";
  386.         tail_offs = 0;
  387. #endif
  388.         break;
  389.     default:
  390.         DEBUG(("Pexec(%d,%lx,%lx,%lx): bad mode",mode,ptr1,ptr2,ptr3));
  391.         return EINVFN;
  392.     }
  393.  
  394.     TRACE((tfmt,mode,ptr1,(char *)ptr2+tail_offs,ptr3));
  395.  
  396. /* Pexec with mode 0x8000 indicates tracing should be active */
  397.     ptrace = (!mkwait && (mode & 0x8000));
  398.  
  399. /* in most cases, we'll want a process struct to exist,
  400.  * so make sure one is around. Note that we must be
  401.  * careful to free it later!
  402.  */
  403.  
  404. TRACE(("Checking for memory for new PROC structure"));
  405.     p = 0;
  406.     if (!overlay) {
  407.         p = new_proc();
  408.         if (!p) {
  409.             DEBUG(("Pexec: couldn't get a PROC struct"));
  410.             return ENSMEM;
  411.         }
  412.     }
  413.  
  414. TRACE(("creating environment"));
  415.  
  416.     if (mkload || mkbase) {
  417.         env = create_env((char *)ptr3, flags);
  418.         if (!env) {
  419.             DEBUG(("Pexec: unable to create environment"));
  420.             if (p) dispose_proc(p);
  421.             return ENSMEM;
  422.         }
  423.     }
  424.  
  425. TRACE(("creating base page"));
  426.  
  427.     if (mkbase) {
  428.         base = create_base((char *)ptr2, env, flags, 0L);
  429.         if (!base) {
  430.             DEBUG(("Pexec: unable to create basepage"));
  431.             detach_region(curproc, env);
  432.             if (p) dispose_proc(p);
  433.             return ENSMEM;
  434.         }
  435. TRACELOW(("Pexec: basepage region(%lx) is %ld bytes at %lx", base, base->len, base->loc));
  436.     }
  437.     else if (mkload) {
  438.         base = load_region((char *)ptr1, env, (char *)ptr2,
  439.             &xattr, &text, &flags);
  440.         if (!base) {
  441.             DEBUG(("Pexec: load_region failed"));
  442.             detach_region(curproc, env);
  443.             if (p) dispose_proc(p);
  444.             return mint_errno;
  445.         }
  446. TRACE(("Pexec: basepage region(%lx) is %ld bytes at %lx", base, base->len, base->loc));
  447.     }
  448.     else {    /* mode == 4,6,104,106,204, or 206 -- just go */
  449.         base = addr2mem((virtaddr)ptr2);
  450.         if (base)
  451.             env = addr2mem(*(void **)(base->loc + 0x2c));
  452.         else
  453.             env = 0;
  454.         if (!env) {
  455.             DEBUG(("Pexec: memory not owned by parent"));
  456.             if (p) dispose_proc(p);
  457.             return EIMBA;
  458.         }
  459. #if 0
  460.       /* make sure that the PC we are about to use is in a region which is
  461.        * attached to the process; this is most commonly a problem for
  462.        * shared text segment programs.
  463.        * BUG: we should verify that the PC is in a region to which the
  464.        * child process should legitimately have access.
  465.        */
  466.               text = addr2region(((BASEPAGE *)base->loc)->p_tbase);
  467.               if (text == base) {
  468.               /* text segment is part of base region */
  469.                       text = NULL;
  470.               }
  471. #endif
  472.     }
  473.  
  474. /* make a local copy of the name, in case we are overlaying the current
  475.  * process
  476.  */
  477.     if (mkname) {
  478.         lastslash = 0;
  479.         newname = ptr1;
  480.         while (*newname) {
  481.             if (*newname == '\\' || *newname == '/')
  482.                 lastslash = newname;
  483.             ++newname;
  484.         }
  485.         if (!lastslash)
  486.             lastslash = ptr1;
  487.         else
  488.             lastslash++;
  489.  
  490.         i = 0; newname = localname;
  491.         while (i++ < PNAMSIZ) {
  492.             if (*lastslash == '.' || *lastslash == 0) {
  493.                 *newname = 0; break;
  494.             }
  495.             else
  496.                 *newname++ = *lastslash++;
  497.         }
  498.         *newname = 0;
  499.     }
  500.  
  501.     if (mkload || mkbase) {
  502.         /*
  503.          * Now that the file's loaded, flags is set to the prgflags
  504.          * for the file.  In the case of mkbase it's been right all along.
  505.          * Here's where we change the protection on the environment to
  506.          * match those flags.
  507.          */
  508.         mark_region(env,(short)((flags & F_PROTMODE) >> F_PROTSHIFT));
  509.     }
  510.  
  511.     if (p) {
  512.     /* free the PROC struct so fork_proc will succeed */
  513.     /* FIXME: it would be much better to pass the PROC as a parameter
  514.      * to fork_proc!!
  515.      */
  516.         dispose_proc(p);
  517.         p = 0;
  518.     }
  519.  
  520.     if (mkgo) {
  521.         BASEPAGE *b;
  522.  
  523.     /* tell the child who the parent was */
  524.         b = (BASEPAGE *)base->loc;
  525.  
  526.         if (overlay) {
  527.             b->p_parent = curproc->base->p_parent;
  528.             p = curproc;
  529.         /* make sure that exec_region doesn't free the base and env */
  530.             base->links++;
  531.             env->links++;
  532.             if (text) text->links++;
  533.         }
  534.         else {
  535.             b->p_parent = curproc->base;
  536.             p = fork_proc();
  537.         }
  538.         if (!p) {
  539.             if (mkbase) {
  540.                 detach_region(curproc, base);
  541.                 detach_region(curproc, env);
  542.                 if (text) detach_region(curproc, text);
  543.             }
  544.             return mint_errno;
  545.         }
  546.         if (ptrace)
  547.             p->ptracer = pid2proc(p->ppid);
  548.  
  549.     /* Even though the file system won't allow unauthorized access
  550.      * to setuid/setgid programs, it's better to err on the side of
  551.      * caution and forbid them to be traced (since the parent can arrange
  552.      * to share the child's address space, not all accesses need to
  553.      * go through the file system.)
  554.      */
  555.         if (mkload && mkgo && !p->ptracer) {    /* setuid/setgid is OK */
  556.             if (xattr.mode & S_ISUID)
  557.                 p->euid = xattr.uid;
  558.             if (xattr.mode & S_ISGID)
  559.                 p->egid = xattr.gid;
  560.         }
  561.     /* exec_region frees the memory attached to p; that's always what
  562.      * we want, since fork_proc duplicates the memory, and since
  563.      * if we didn't call fork_proc then we're overlaying.
  564.      * NOTE: after this call, we may not be able to access the
  565.      * original address space that the Pexec was taking place in
  566.      * (if this is an overlaid Pexec, we just freed that memory).
  567.      */
  568.         (void)exec_region(p, base, thread);
  569.         attach_region(p, env);
  570.         attach_region(p, base);
  571.         if (text) attach_region(p, text);
  572.  
  573.         if (mkname) {
  574.     /* interesting coincidence -- if a process needs a name, it usually
  575.      * needs to have its domain reset to DOM_TOS. Doing it this way
  576.      * (instead of doing it in exec_region) means that Pexec(4,...)
  577.      * can be used to create new threads of execution which retain
  578.      * the same domain.
  579.      */
  580.             if (!thread)
  581.                 p->domain = DOM_TOS;
  582.  
  583.     /* put in the new process name we saved above */
  584.             strcpy(p->name, localname);
  585.         }
  586.  
  587.     /* turn on tracing for the new process */
  588.         if (p->ptracer)
  589.             p->ctxt[CURRENT].ptrace = 1;
  590.  
  591.     /* jr: add Pexec information to PROC struct */
  592.     p->cmdlin[0] = 0;
  593.     if (mkbase || mkload)
  594.         strncpy(p->cmdlin, ptr2, 128);
  595.     p->fname[0] = 0;
  596.     if (mkload)
  597.     {
  598.         char tmp[PATH_MAX];
  599.         char *source = ptr1;
  600.         tmp[1] = ':';
  601.         if (source[1] == ':') {
  602.             tmp[0] = source[0];
  603.             source += 2;
  604.         } else
  605.             tmp[0] = 'A' + curproc->curdrv;
  606.         if (DIRSEP(source[0]))    /* absolute path? */
  607.         {
  608.             strncpy (&tmp[2], &source[0], PATH_MAX-2);
  609.             strcpy (p->fname, tmp);
  610.         } else {
  611.             if (! d_getcwd (&tmp[2], tmp[0] - 'A' + 1, PATH_MAX - 2))
  612.                 ksprintf (p->fname, "%s\\%s", tmp, source);
  613.         }
  614.     }
  615.  
  616.     /* set the time/date stamp of u:\proc */
  617.         proctime = timestamp;
  618.         procdate = datestamp;
  619.  
  620.         if (overlay) {
  621.             /* correct for temporary increase in links (see above) */
  622.             base->links--;
  623.             env->links--;
  624.             if (text) text->links--;
  625.             /* let our parent run, if it Vfork'd() */
  626.             if ( (p = pid2proc(curproc->ppid)) != 0 ) {
  627.                 if (p->wait_q == WAIT_Q && 
  628.                     p->wait_cond == (long)curproc) {
  629.                     short sr = spl7();
  630.                     rm_q(WAIT_Q, p);
  631.                     add_q(READY_Q, p);
  632.                     spl(sr);
  633.                 }
  634.             }
  635.  
  636.         /* OK, let's run our new code */
  637.         /* we guarantee ourselves at least 2 timeslices to do an Mshrink */
  638.             assert(curproc->magic == CTXT_MAGIC);
  639.             fresh_slices(2);
  640.             leave_kernel();
  641.             change_context(&(curproc->ctxt[CURRENT]));
  642.         }
  643.         else {
  644.     /* we want this process to run ASAP */
  645.     /* so we temporarily give it high priority and put it first on the
  646.      * run queue
  647.      */
  648.             run_next(p, 2);
  649.         }
  650.     }
  651.  
  652.     if (mkfree) {
  653.         detach_region(curproc, base);
  654.         detach_region(curproc, env);
  655.         if (text) detach_region(curproc, text);
  656.     }
  657.  
  658.     if (mkwait) {
  659.         long oldsigint, oldsigquit;
  660.  
  661.         oldsigint = curproc->sighandle[SIGINT];
  662.         oldsigquit = curproc->sighandle[SIGQUIT];
  663.         curproc->sighandle[SIGINT] =
  664.              curproc->sighandle[SIGQUIT] = SIG_IGN;
  665.  
  666.         newpid = p->pid;
  667.         for(;;) {
  668.             r = p_wait3(0, (long *)0);
  669.             if (r < 0) {
  670.                 ALERT("p_exec: wait error");
  671.                 return EINTRN;
  672.             }
  673.             if ( newpid == ((r&0xffff0000L) >> 16) ) {
  674.                 TRACE(("leaving Pexec; child return code %ld", r));
  675.                 r = r & 0x0000ffffL;
  676.                 break;
  677.             }
  678.             if (curproc->pid)
  679.                 DEBUG(("Pexec: wrong child found"));
  680.         }
  681.         curproc->sighandle[SIGINT] = oldsigint;
  682.         curproc->sighandle[SIGQUIT] = oldsigquit;
  683.         return r;
  684.     }
  685.     else if (mkgo) {
  686.     /* warning: after the yield() the "p" structure may not exist any more
  687.      * (if the child exits right away)
  688.      */
  689.         newpid = p->pid;
  690.         yield();    /* let the new process run */
  691.         return newpid;
  692.     } else {
  693.         TRACE(("leaving Pexec with basepage address %lx", base->loc));
  694.         return base->loc;
  695.     }
  696. }
  697.  
  698. /*
  699.  * terminate a process, with return code "code". If que == ZOMBIE_Q, free
  700.  * all resources attached to the child; if que == TSR_Q, free everything
  701.  * but memory.
  702.  * NOTE: terminate() should be called only when the process is to be
  703.  * "terminated with extreme prejuidice". Most times, p_term or p_termres
  704.  * are the functions to use, since they allow the user to do some cleaning
  705.  * up, etc.
  706.  */
  707.  
  708. long
  709. terminate(code, que)
  710.     int code, que;
  711. {
  712.     extern PROC *dlockproc[];    /* in dosdir.c */
  713.     PROC *p;
  714.     FILEPTR *fp;
  715.     MEMREGION *m;
  716.     MEMREGION **hold_mem;
  717.     virtaddr *hold_addr;
  718.     int  i, wakemint = 0;
  719.     DIR *dirh, *nexth;
  720.     extern short bconbsiz;    /* in bios.c */
  721.  
  722.     if (bconbsiz)
  723.         (void) bflush();
  724.  
  725.     assert(que == ZOMBIE_Q || que == TSR_Q);
  726.  
  727.     if (curproc->pid == 0) {
  728.         FATAL("attempt to terminate MiNT");
  729.     }
  730.  
  731. /* cancel all pending timeouts for this process */
  732.     cancelalltimeouts();
  733. /* cancel alarm clock */
  734.     curproc->alarmtim = 0;
  735.  
  736. /* release any drives locked by Dlock */
  737.     for(i = 0; i < NUM_DRIVES; i++) {
  738.         if (dlockproc[i] == curproc) {
  739.             dlockproc[i] = 0;
  740.             changedrv(i);
  741.         }
  742.     }
  743.  
  744. /* release the controlling terminal, if we're a process group leader */
  745.     fp = curproc->handle[-1];
  746.     if (fp && is_terminal(fp) && curproc->pgrp == curproc->pid) {
  747.         struct tty *tty = (struct tty *)fp->devinfo;
  748.         if (curproc->pgrp == tty->pgrp)
  749.             tty->pgrp = 0;
  750.     }
  751.  
  752. /* close all files */
  753.     for (i = MIN_HANDLE; i < MAX_OPEN; i++) {
  754.         if ((fp = curproc->handle[i]) != 0)
  755.             do_close(fp);
  756.         curproc->handle[i] = 0;
  757.     }
  758.  
  759. /* close any unresolved Fsfirst/Fsnext directory searches */
  760.     for (i = 0; i < NUM_SEARCH; i++) {
  761.         if (curproc->srchdta[i]) {
  762.             DIR *dirh = &curproc->srchdir[i];
  763.             (*dirh->fc.fs->closedir)(dirh);
  764.             release_cookie(&dirh->fc);
  765.             dirh->fc.fs = 0;
  766.         }
  767.     }
  768.  
  769. /* close pending opendir/readdir searches */
  770.     for (dirh = curproc->searches; dirh; ) {
  771.         if (dirh->fc.fs) {
  772.             (*dirh->fc.fs->closedir)(dirh);
  773.             release_cookie(&dirh->fc);
  774.         }
  775.         nexth = dirh->next;
  776.         kfree(dirh);
  777.         dirh = nexth;
  778.     }
  779.  
  780. /* release the directory cookies held by the process */
  781.     for (i = 0; i < NUM_DRIVES; i++) {
  782.         release_cookie(&curproc->curdir[i]);
  783.         curproc->curdir[i].fs = 0;
  784.         release_cookie(&curproc->root[i]);
  785.         curproc->root[i].fs = 0;
  786.     }
  787.  
  788. /* release all semaphores owned by this process */
  789.     free_semaphores(curproc->pid);
  790.  
  791. /* free all memory */
  792. /* if mflags & M_KEEP then attach it to process 0 */
  793.     if (que == ZOMBIE_Q) {
  794.         for (i = curproc->num_reg - 1; i >=0; i--) {
  795.             m = curproc->mem[i];
  796.             curproc->mem[i] = 0; curproc->addr[i] = 0;
  797.             if (m) {
  798.         /* don't free specially allocated memory */
  799.                 if (m->mflags & M_KEEP) {
  800.                     if (curproc != rootproc)
  801.                         attach_region(rootproc, m);
  802.                 }
  803.                 m->links--;
  804.                 if (m->links == 0) {
  805.                     free_region(m);
  806.                 }
  807.             }
  808.         }
  809.  
  810.         /*
  811.          * mark the mem & addr arrays as void so the memory
  812.          * protection code won't try to walk them. Do this before
  813.          * freeing them so we don't try to walk them when marking
  814.          * those pages themselves as free!
  815.          *
  816.          * Note: when a process terminates, the MMU root pointer
  817.          * still points to that process' page table, until the next
  818.          * process is dispatched.  This is OK, since the process'
  819.          * page table is in system memory, and it isn't going to be
  820.          * freed.  It is going to wind up on the free process list,
  821.          * though, after dispose_proc. This might be Not A Good
  822.          * Thing.
  823.          */
  824.  
  825.         hold_addr = curproc->addr;
  826.         hold_mem = curproc->mem;
  827.  
  828.         curproc->mem = NULL;
  829.         curproc->addr = NULL;
  830.         curproc->num_reg = 0;
  831.  
  832.         kfree(hold_addr);
  833.         kfree(hold_mem);
  834.     }
  835. /*    else
  836.          make TSR process non-swappable */
  837.  
  838. /*
  839.  * make sure that any open files that refer to this process are
  840.  * closed
  841.  */
  842.     changedrv(PROC_BASE_DEV | curproc->pid);
  843.  
  844. /* find our parent (if parent not found, then use process 0 as parent
  845.  * since that process is constantly in a wait loop)
  846.  */
  847.  
  848.     p = pid2proc(curproc->ppid);
  849.     if (!p) {
  850.         TRACE(("terminate: parent not found"));
  851.         p = pid2proc(0);
  852.     }
  853.  
  854. /* NOTE: normally just post_sig is sufficient for sending a signal; but
  855.  * in this particular case, we have to worry about processes that are
  856.  * blocking all signals because they Vfork'd and are waiting for us to
  857.  * finish (which is indicated by a wait_cond matching our PROC
  858.  * structure), and also processes that are ignoring SIGCHLD but are
  859.  * waiting for us.
  860.  */
  861.     if (p->wait_q == WAIT_Q && 
  862.         (p->wait_cond == (long)curproc || p->wait_cond == (long)p_waitpid) ) {
  863.         short sr = spl7();
  864.         TRACE(("terminate: waking up parent"));
  865.         rm_q(WAIT_Q, p);
  866.         add_q(READY_Q, p);
  867.         spl(sr);
  868.     }
  869.     if (curproc->ptracer && curproc->ptracer != p) {
  870.         /* BUG: should we ensure curproc->ptracer is awake ? */
  871.         post_sig(curproc->ptracer, SIGCHLD);    /* tell tracing process */
  872.     }
  873.     post_sig(p, SIGCHLD);        /* inform of process termination */
  874.  
  875. /* find our children, and orphan them
  876.  * also, check for processes we were tracing, and
  877.  * cancel the trace
  878.  */
  879.     i = curproc->pid;
  880.     for (p = proclist; p; p = p->gl_next) {
  881.         if (p->ppid == i) {
  882.             p->ppid = 0;    /* have the system adopt it */
  883.             if (p->wait_q == ZOMBIE_Q) 
  884.                 wakemint = 1;    /* we need to wake proc. 0 */
  885.         }
  886.         if (p->ptracer == curproc) {
  887.             p->ptracer = 0;
  888. /*
  889.  * `FEATURE': we terminate traced processes when the tracer terminates.
  890.  * It might plausibly be argued that it would be better to let them
  891.  * continue, to let some (new) tracer take them over. On the other hand,
  892.  * if the tracer terminated normally, it should have used Fcntl(PTRACESFLAGS)
  893.  * to reset the trace nicely, so something must be wrong for us to have
  894.  * reached here.
  895.  */
  896.             post_sig(p, SIGTERM);    /* arrange for termination */
  897.         }
  898.     }
  899.  
  900.     if (wakemint) {
  901.         p = rootproc;        /* pid 0 */
  902.         if (p->wait_q == WAIT_Q) {
  903.             short sr = spl7();
  904.             rm_q(WAIT_Q, p);
  905.             add_q(READY_Q, p);
  906.             spl(sr);
  907.         }
  908.     }
  909.  
  910. /* this makes sure that our children are inherited by the system;
  911.  * plus, it may help avoid problems if somehow a signal gets
  912.  * through to us
  913.  */
  914.     for(i = 0; i < NSIG; i++)
  915.         curproc->sighandle[i] = SIG_IGN;
  916.  
  917. /* finally, reset the time/date stamp for u:\proc */
  918.     proctime = timestamp;
  919.     procdate = datestamp;
  920.  
  921.     sleep(que, (long)(unsigned)code);
  922.  
  923. /* we shouldn't ever get here */
  924.     FATAL("terminate: sleep woke up when it shouldn't have");
  925.     return 0;
  926. }
  927.  
  928. /*
  929.  * TOS process termination entry points:
  930.  * p_term terminates the process, freeing its memory
  931.  * p_termres lets the process hang around resident in memory, after
  932.  * shrinking its transient program area to "save" bytes
  933.  */
  934.  
  935. long ARGS_ON_STACK
  936. p_term(code)
  937.     int code;
  938. {
  939.     CONTEXT *syscall;
  940.  
  941.     TRACE(("Pterm(%d)", code));
  942. /* call the process termination vector */
  943.     syscall = &curproc->ctxt[SYSCALL];
  944.  
  945.     if (syscall->term_vec != (long)rts) {
  946.         TRACE(("term_vec: user has something to do"));
  947. /*
  948.  * we handle the termination vector just like Supexec(), by
  949.  * sending signal 0 to the process. See supexec() in xbios.c for details.
  950.  * Note that we _always_ want to unwind the signal stack, and setting
  951.  * bit 1 of curproc->sigmask tells handle_sig to do that -- see signal.c.
  952.  */
  953.         curproc->sigmask |= 1L;
  954.         (void)supexec((Func)syscall->term_vec, 0L, 0L, 0L, 0L, 
  955.                 (long)code);
  956. /*
  957.  * if we arrive here, continue with the termination...
  958.  */
  959.     }
  960.     return terminate(code, ZOMBIE_Q);
  961. }
  962.  
  963. long ARGS_ON_STACK
  964. p_term0()
  965. {
  966.     return p_term(0);
  967. }
  968.  
  969. long ARGS_ON_STACK
  970. p_termres(save, code)
  971.     long save;
  972.     int code;
  973. {
  974.     MEMREGION *m;
  975.     int i;
  976.  
  977.     TRACE(("Ptermres(%ld, %d)", save, code));
  978.     m = curproc->mem[1];    /* should be the basepage (0 is env.) */
  979.     if (m) {
  980.         (void)shrink_region(m, save);
  981.     }
  982. /*
  983.  * make all of the TSR's private memory globally accessible;
  984.  * this means that more TSR's will "do the right thing"
  985.  * without having to have prgflags set.
  986.  */
  987.     for (i = 0; i < curproc->num_reg; i++) {
  988.         m = curproc->mem[i];
  989.         if (m && m->links == 1) {    /* only the TSR is owner */
  990.             if (get_prot_mode(m) == PROT_P) {
  991.                 mark_region(m, PROT_G);
  992.             }
  993.         }
  994.     }
  995.     return terminate(code, TSR_Q);
  996. }
  997.  
  998. /*
  999.  * routine for waiting for children to die. Return has the pid of the
  1000.  * found child in the high word, and the child's exit code in
  1001.  * the low word. If no children exist, return "File Not Found".
  1002.  * If (nohang & 1) is nonzero, then return a 0 immediately if we have
  1003.  * no dead children but some living ones that we still have to wait
  1004.  * for. If (nohang & 2) is nonzero, then we return any stopped
  1005.  * children; otherwise, only children that have exited or are stopped
  1006.  * due to a trace trap are returned.
  1007.  * If "rusage" is non-zero and a child is found, put the child's
  1008.  * resource usage into it (currently only the user and system time are
  1009.  * sent back).
  1010.  * The pid argument specifies a set of child processes for which status
  1011.  * is requested:
  1012.  *     If pid is equal to -1, status is requested for any child process.
  1013.  *
  1014.  *    If pid is greater than zero, it specifies the process ID of a
  1015.  *    single child process for which status is requested.
  1016.  *
  1017.  *    If pid is equal to zero, status is requested for any child
  1018.  *    process whose process group ID is equal to that of the calling
  1019.  *    process.
  1020.  *
  1021.  *    If pid is less than -1, status is requested for any child process
  1022.  *    whose process group ID is equal to the absolute value of pid.
  1023.  *
  1024.  * Note this call is a real standard crosser... POSIX.1 doesn't have the
  1025.  * rusage stuff, BSD doesn't have the pid stuff; both are useful, so why
  1026.  * not have it all!
  1027.  */
  1028.  
  1029. long ARGS_ON_STACK
  1030. p_waitpid(pid, nohang, rusage)
  1031.     int pid;
  1032.     int nohang;
  1033.     long *rusage;
  1034. {
  1035.     long r;
  1036.     PROC *p, *q;
  1037.     int ourpid;
  1038.     int found;
  1039.  
  1040.     TRACE(("Pwaitpid(%d, %d, %lx)", pid, nohang, rusage));
  1041.     ourpid = curproc->pid;
  1042.  
  1043. /* if there are terminated children, clean up and return their info;
  1044.  * if there are children, but still running, wait for them;
  1045.  * if there are no children, return an error
  1046.  */
  1047.  
  1048.     do {
  1049. /* look for any children */
  1050.         found = 0;
  1051.         for (p = proclist; p; p = p->gl_next) {
  1052.             if ((p->ppid == ourpid || p->ptracer == curproc) &&
  1053.                 (pid == -1 ||
  1054.                 (pid > 0 && pid == p->pid) ||
  1055.                 (pid == 0 && p->pgrp == ourpid) ||
  1056.                 (pid < -1 && p->pgrp == -pid))) {
  1057.                 found++;
  1058.                 if (p->wait_q == ZOMBIE_Q || p->wait_q == TSR_Q)
  1059.                     break;
  1060.  
  1061. /* p->wait_cond == 0 if a stopped process has already been waited for */
  1062.                 if (p->wait_q == STOP_Q && p->wait_cond) {
  1063.                     if ((nohang & 2) ||
  1064.                     ((p->wait_cond&0x1f00) == (SIGTRAP<<8)))
  1065.                         break;
  1066.                 }
  1067.             }
  1068.         }
  1069.         if (!p) {
  1070.             if (found) {
  1071.                 if (nohang & 1)
  1072.                     return 0;
  1073.                 if (curproc->pid)
  1074.                     TRACE(("Pwaitpid: going to sleep"));
  1075.                 sleep(WAIT_Q, (long)p_waitpid);
  1076.             }
  1077.             else {
  1078.                 DEBUG(("Pwaitpid: no children found"));
  1079.                 return EFILNF;
  1080.             }
  1081.         }
  1082.     } while (!p);
  1083.  
  1084. /* OK, we've found our child */
  1085. /* calculate the return code from the child's exit code and pid */
  1086.     r = (((unsigned long)p->pid) << 16) | (p->wait_cond & 0x0000ffff);
  1087.  
  1088. /* check resource usage */
  1089.     if (rusage) {
  1090.         *rusage++ = p->usrtime;
  1091.         *rusage = p->systime;
  1092.     }
  1093.  
  1094. /* avoid adding adopted trace processes usage to the foster parent */
  1095.     if (curproc->pid == p->ppid) {
  1096.     /* add child's resource usage to parent's */
  1097.         if (p->wait_q == TSR_Q || p->wait_q == ZOMBIE_Q) {
  1098.             curproc->chldstime += p->systime;
  1099.             curproc->chldutime += p->usrtime;
  1100.         }
  1101.     }
  1102.  
  1103. /* if it was stopped, mark it as having been found and again return */
  1104.     if (p->wait_q == STOP_Q) {
  1105.         p->wait_cond = 0;
  1106.         return r;
  1107.     }
  1108.  
  1109. /* We have to worry about processes which attach themselves to running
  1110.  * processes which they want to trace. We fix things up so that the
  1111.  * second time the signal gets delivered we will go all the way to the
  1112.  * end of this function.
  1113.  */
  1114.      if (p->ptracer && p->ptracer->pid != p->ppid) {
  1115.         if (curproc == p->ptracer) {
  1116.         /* deliver the signal to the tracing process first */
  1117.             TRACE(("Pwaitpid(ptracer): returning status to tracing process"));
  1118.             p->ptracer = NULL;
  1119.             return r;
  1120.         }
  1121.         else {
  1122.         /* Hmmm, the real parent got here first */
  1123.             TRACE(("Pwaitpid(ptracer): returning status to parent process"));
  1124.             p->ppid = -1;
  1125.             return r;
  1126.         }
  1127.     }
  1128.     
  1129. /* if it was a TSR, mark it as having been found and return */
  1130.     if (p->wait_q == TSR_Q) {
  1131.         p->ppid = -1;
  1132.         return r;
  1133.     }
  1134.  
  1135. /* it better have been on the ZOMBIE queue from here on in... */
  1136.     assert(p->wait_q == ZOMBIE_Q);
  1137.     assert(p != curproc);
  1138.  
  1139. /* take the child off both the global and ZOMBIE lists */
  1140.     rm_q(ZOMBIE_Q, p);
  1141.  
  1142.     if (proclist == p) {
  1143.         proclist = p->gl_next;
  1144.         p->gl_next = 0;
  1145.     }
  1146.     else {
  1147.         q = proclist;
  1148.         while(q && q->gl_next != p)
  1149.             q = q->gl_next;
  1150.         assert(q);
  1151.         q->gl_next = p->gl_next;
  1152.         p->gl_next = 0;
  1153.     }
  1154.  
  1155.     dispose_proc(p);    /* free the PROC structure */
  1156.  
  1157.     return r;
  1158. }
  1159.  
  1160. /* p_wait3: BSD process termination primitive, here to maintain
  1161.  * compatibility with existing binaries.
  1162.  */
  1163. long ARGS_ON_STACK
  1164. p_wait3(nohang, rusage)
  1165.     int nohang;
  1166.     long *rusage;
  1167. {
  1168.     return p_waitpid(-1, nohang, rusage);
  1169. }
  1170.  
  1171. /* p_wait: block until a child has exited, and don't worry about
  1172.    resource stats. this is provided as a convenience, and to maintain
  1173.    compatibility with existing binaries (yes, I'm lazy...). we could
  1174.    make do with Pwaitpid().
  1175.  */
  1176.  
  1177. long ARGS_ON_STACK
  1178. p_wait()
  1179. {
  1180. /*
  1181.  * BEWARE:
  1182.  * POSIX says that wait() should be implemented as
  1183.  * Pwaitpid(-1, 0, (long *)0). Pwait is really not
  1184.  * useful for much at all, but we'll keep it around
  1185.  * for a while (with it's old, crufty semantics)
  1186.  * for backwards compatibility. People implementing
  1187.  * POSIX style libraries should use Pwaitpid even
  1188.  * to implement wait().
  1189.  */
  1190.     return p_wait3(2, (long *)0);
  1191. }
  1192.  
  1193. /*
  1194.  * do_vfork(save): create a duplicate of  the current process. This is
  1195.  * essentially a vfork() algorithm, except that if (save == 1) the
  1196.  * parent's address space is saved, and then restored when the process
  1197.  * is made runnable again. The parent is suspended until either the child
  1198.  * process (the duplicate) exits or does a Pexec which overlays its
  1199.  * memory space.
  1200.  *
  1201.  * "txtsize" is the size of the process' TEXT area, if it has a valid one;
  1202.  * this is part of the second memory region attached (the basepage one)
  1203.  * and need not be saved (we assume processes don't write on their own
  1204.  * code segment)
  1205.  */
  1206.  
  1207. static long
  1208. do_vfork(save)
  1209.     int save;
  1210. {
  1211.     PROC *p;
  1212.     long sigmask;
  1213.     MEMREGION *m, *savemem = 0;
  1214.     long savesize, txtsize;
  1215.     int i, newpid;
  1216.     char *saveplace;
  1217.  
  1218.     p = fork_proc();
  1219.     if (!p) {
  1220.         DEBUG(("do_vfork: couldn't get new PROC struct"));
  1221.         return mint_errno;
  1222.     }
  1223. /* set u:\proc time+date */
  1224.     proctime = timestamp;
  1225.     procdate = datestamp;
  1226.  
  1227. /*
  1228.  * maybe save the parent's address space
  1229.  */
  1230.     txtsize = p->txtsize;
  1231.  
  1232.     if (save) {
  1233.         TRACE(("do_vfork: saving parent"));
  1234.         savesize = memused(curproc) - txtsize;
  1235.         assert(savesize >= 0);
  1236.  
  1237.         saveplace = (char *)alloc_region(alt, savesize, PROT_P);
  1238.         if (!saveplace)
  1239.             saveplace = (char *)alloc_region(core, savesize, PROT_P);
  1240.  
  1241.         if (!saveplace) {
  1242.             DEBUG(("do_vfork: can't save parent's memory"));
  1243.             p->ppid = 0;        /* abandon the child */
  1244.             post_sig(p, SIGKILL);    /* then kill it */
  1245.             p->ctxt[CURRENT].pc = (long)check_sigs;
  1246.                 /* just to make sure it dies */
  1247.             p->ctxt[CURRENT].ssp = (long)(p->stack + ISTKSIZE);
  1248.             p->pri = MAX_NICE+1;
  1249.             run_next(p, 1);
  1250.             yield();
  1251.             return ENSMEM;
  1252.         }
  1253.         savemem = addr2mem((virtaddr)saveplace);
  1254.         assert(savemem);
  1255.         for (i = 0; i < curproc->num_reg; i++) {
  1256.             m = curproc->mem[i];
  1257.             if (m && m != savemem && !(m->mflags & M_SHTEXT)) {
  1258.                 if (i != 1 || txtsize == 0) {
  1259.                     quickmove(saveplace, (char *)m->loc, m->len);
  1260.                     saveplace += m->len;
  1261.                 }
  1262.                 else {
  1263.                     quickmove(saveplace, (char *)m->loc+txtsize,
  1264.                     m->len - txtsize);
  1265.                     saveplace += m->len - txtsize;
  1266.                 }
  1267.             }
  1268.         }
  1269.     }
  1270.                 
  1271.     p->ctxt[CURRENT] = p->ctxt[SYSCALL];
  1272.     p->ctxt[CURRENT].regs[0] = 0;    /* child returns a 0 from call */
  1273.     p->ctxt[CURRENT].sr &= ~(0x2000); /* child must be in user mode */
  1274. #if 0        /* set up in fork_proc() */
  1275.     p->ctxt[CURRENT].ssp = (long)(p->stack + ISTKSIZE);
  1276. #endif
  1277.  
  1278. /* watch out for job control signals, since our parent can never wake
  1279.  * up to respond to them. solution: block them; exec_region (in mem.c)
  1280.  * clears the signal mask, so an exec() will unblock them.
  1281.  */
  1282.     p->sigmask |= (1L << SIGTSTP) | (1L << SIGTTIN) | (1L << SIGTTOU);
  1283.  
  1284.     TRACE(("do_vfork: parent going to sleep, wait_cond == %lx",
  1285.         (long)p));
  1286.  
  1287. /* WARNING: This sleep() must absolutely not wake up until the child
  1288.  * has released the memory space correctly. That's why we mask off
  1289.  * all signals.
  1290.  */
  1291.     sigmask = curproc->sigmask;
  1292.     curproc->sigmask = ~((unsigned long)1 << SIGKILL);
  1293.  
  1294.     add_q(READY_Q, p);        /* put it on the ready queue */
  1295.     sleep(WAIT_Q, (long)p);            /* while we wait for it */
  1296.     TRACE(("do_vfork: parent waking up"));
  1297.  
  1298.     if (save) {
  1299.         TRACE(("do_vfork: parent restoring memory"));
  1300.         saveplace = (char *)savemem->loc;
  1301.         for (i = 0; i < curproc->num_reg; i++) {
  1302.             m = curproc->mem[i];
  1303.             if (m && (m != savemem) && !(m->mflags & M_SHTEXT)) {
  1304.                 if (i != 1 || txtsize == 0) {
  1305.                     quickmove((char *)m->loc, saveplace, m->len);
  1306.                     saveplace += m->len;
  1307.                 }
  1308.                 else {
  1309.                     quickmove((char *)m->loc+txtsize, saveplace,
  1310.                     m->len - txtsize);
  1311.                     saveplace += m->len - txtsize;
  1312.                 }
  1313.             }
  1314.         }
  1315.         detach_region(curproc, savemem);
  1316.     }
  1317.     curproc->sigmask = sigmask;
  1318. /* note that the PROC structure pointed to by p may be freed during
  1319.  * the check_sigs call!
  1320.  */
  1321.     newpid = p->pid;
  1322.     check_sigs();    /* did we get any signals while sleeping? */
  1323.     return newpid;
  1324. }
  1325.  
  1326. /*
  1327.  * here are the interfaces that the user sees. Pvfork() doesn't save
  1328.  * the child's address space; Pfork() does. Someday Pfork() should
  1329.  * allow asynchronous execution of both child and parent, but this
  1330.  * will do for now.
  1331.  */
  1332.  
  1333. long ARGS_ON_STACK
  1334. p_vfork()
  1335. {
  1336.     return do_vfork(0);
  1337. }
  1338.  
  1339. long ARGS_ON_STACK
  1340. p_fork()
  1341. {
  1342.     return do_vfork(1);
  1343. }
  1344.